import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import java.awt.*;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by IntelliJ IDEA.
 * User: xsicrz
 * Date: 6/6/11
 * Time: 8:21 AM
 * To change this template use File | Settings | File Templates.
 */
public class TimelineTableCellRenderer extends DefaultTableCellRenderer {

    private TimelineTableModel tableModel;

    public TimelineTableCellRenderer(TimelineTableModel myTableModel) {
        this.tableModel = myTableModel;
    }

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        setBackground(Color.WHITE);
        setForeground(Color.BLACK);
        if (row < tableModel.getDaysOfMonth().size()) {
            Date dayOfMonth = tableModel.getDaysOfMonth().get(row);
            Calendar cal = Calendar.getInstance();
            cal.setTime(dayOfMonth);
            int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK);

            if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) {
                setBackground(Color.LIGHT_GRAY);
            }
            int dayOfYear = cal.get(Calendar.DAY_OF_MONTH);
            int month = cal.get(Calendar.MONTH);
            int year = cal.get(Calendar.YEAR);
            if (column == 7) {
                Saisie saisie = tableModel.getModel().getSaisie(dayOfYear, month, year);
                if (saisie != null) {
                    if (saisie.getDifferenceEnMillis() < 0) {
                        setForeground(Color.RED);
                    }
                }
            }


        } else if (row == tableModel.getDaysOfMonth().size()) {
            if (column == 7) {
                long differenceInMillis = tableModel.getModel().getDifferenceForMonthAndYear(tableModel.getDaysOfMonth());
                if (differenceInMillis < 0) {
                    setForeground(Color.RED);
                }
            }
        }
        return super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
    }
}
